package com.freeter.modules.gjs.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.freeter.modules.gjs.entity.MstrSite;
import com.freeter.modules.gjs.mapper.MstrSiteMapper;
import com.freeter.modules.gjs.service.MstrSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class MstrSiteServiceImpl implements MstrSiteService {
    @Autowired
    private MstrSiteMapper mstrSiteMapper;

    public List<MstrSite> getSitesByCondition(String projectCode, Integer isDeleted) {
        Wrapper<MstrSite> wrapper = new EntityWrapper<>();
        if (projectCode != null) wrapper.eq("project_code", projectCode);
        if (isDeleted != null) wrapper.eq("is_deleted", isDeleted);
        return mstrSiteMapper.selectList(wrapper);
    }

    @Override
    public List<MstrSite> getAllSites() {
        Wrapper<MstrSite> wrapper = new EntityWrapper<>();
        wrapper.eq("is_deleted", 0);
        return mstrSiteMapper.selectList(wrapper);
    }

    @Override
    public MstrSite getSiteById(Long id) {
        if (id == null) {
            return null;
        }
        return mstrSiteMapper.selectById(id);
    }

    @Override
    public MstrSite getSiteByCode(String siteCode) {
        if (siteCode == null) {
            return null;
        }
        Wrapper<MstrSite> wrapper = new EntityWrapper<>();
        wrapper.eq("project_code", siteCode).eq("is_deleted", 0);
        List<MstrSite> sites = mstrSiteMapper.selectList(wrapper);
        return sites.isEmpty() ? null : sites.get(0);
    }

    @Override
    public MstrSite createSite(MstrSite site) {
        if (site == null) {
            return null;
        }
        // 设置默认值
        if (site.getIsDeleted() == null) {
            site.setIsDeleted(0);
        }
        mstrSiteMapper.insert(site);
        return site;
    }

    @Override
    public MstrSite updateSite(Long id, MstrSite site) {
        if (id == null || site == null) {
            return null;
        }
        
        // 检查站点是否存在
        MstrSite existingSite = mstrSiteMapper.selectById(id);
        if (existingSite == null) {
            return null;
        }
        
        // 更新字段
        site.setId(id);
        mstrSiteMapper.updateById(site);
        return mstrSiteMapper.selectById(id);
    }

    @Override
    public void deleteSite(Long id) {
        if (id == null) {
            return;
        }
        
        // 逻辑删除，设置 is_deleted = 1
        MstrSite site = new MstrSite();
        site.setId(id);
        site.setIsDeleted(1);
        mstrSiteMapper.updateById(site);
    }

    @Override
    public void deleteSiteByCode(String siteCode) {
        if (siteCode == null) {
            return;
        }
        
        // 根据项目代码查找并逻辑删除
        Wrapper<MstrSite> wrapper = new EntityWrapper<>();
        wrapper.eq("project_code", siteCode).eq("is_deleted", 0);
        List<MstrSite> sites = mstrSiteMapper.selectList(wrapper);
        
        for (MstrSite site : sites) {
            site.setIsDeleted(1);
            mstrSiteMapper.updateById(site);
        }
    }

    @Override
    public List<MstrSite> getSitesByStatus(String status) {
        if (status == null) {
            return getAllActiveSites();
        }
        
        Wrapper<MstrSite> wrapper = new EntityWrapper<>();
        wrapper.eq("is_deleted", 0);
        
        // 根据状态过滤，这里假设状态信息存储在 site_info 字段中
        // 如果状态字段不同，需要相应调整
        if (!status.isEmpty()) {
            wrapper.like("site_info", status);
        }
        
        return mstrSiteMapper.selectList(wrapper);
    }

    @Override
    public List<MstrSite> getSitesByProjectCodes(List<String> projectCodes) {
        if (projectCodes == null || projectCodes.isEmpty()) {
            return getAllActiveSites();
        }
        
        Wrapper<MstrSite> wrapper = new EntityWrapper<>();
        wrapper.in("project_code", projectCodes).eq("is_deleted", 0);
        return mstrSiteMapper.selectList(wrapper);
    }

    public List<MstrSite> getAllActiveSites() {
        Wrapper<MstrSite> wrapper = new EntityWrapper<>();
        wrapper.eq("is_deleted", 0);
        return mstrSiteMapper.selectList(wrapper);
    }
    
    // 其他业务方法同理重构
} 